# -*- coding: UTF-8 -*-
# 飞机大战游戏
import pygame,sys
from pygame.locals import *
import time
import random

class HeroPlane:
    '''玩家飞机'''
    def __init__(self,screen_temp):
        self.x=200
        self.y=400
        self.screen=screen_temp
        self.image=pygame.image.load("images/me.png")
        self.bullet_list=[]
    def display(self):
        '''绘制飞机'''
        # 绘制子弹
        for b in self.bullet_list:
            b.display()
            if b.move(): # 子弹移出屏幕后，销毁子弹
                self.bullet_list.remove(b)
        self.screen.blit(self.image,(self.x,self.y))

    def checkBump(self,enemylist):
        '''检测与敌机子弹碰撞'''
        # 遍历所有敌机、敌机子弹
        for em in enemylist:
            for blt in em.bullet_list:
                if blt.x > self.x + 12 and blt.x < self.x + 92 and blt.y > self.y + 10 and blt.y < self.y + 90:
                    return True

    def move_left(self):
        '''左移动飞机'''
        self.x-=15
        if self.x<=0:
            self.x=0
    def move_right(self):
        '''右移动飞机'''
        self.x+=15
        if self.x>=406:
            self.x=406

    def fire(self):
        self.bullet_list.append(Bullet(self.screen,self.x,self.y))
        # print(len(self.bullet_list))

class Bullet:
    '''子弹'''
    def __init__(self,screen_temp,x,y):
        self.x=x+53
        self.y=y
        self.screen=screen_temp
        self.image=pygame.image.load("images/pd.png")

    def display(self):
        '''绘制子弹'''
        self.screen.blit(self.image,(self.x,self.y))

    def move(self):
        self.y-=10
        if self.y<=-20:
            return True

class Bullet_Enemy:
    '''敌机子弹'''
    def __init__(self,screen_temp,x,y):
        self.x=x+53
        self.y=y
        self.screen=screen_temp
        self.image=pygame.image.load("images/pd.png")

    def display(self):
        '''绘制子弹'''
        self.screen.blit(self.image,(self.x,self.y))
        # print((self.x,self.y))

    def move(self):
        self.y+=6
        if self.y>=516:
            return True

class EnemyPlane:
    '''敌机'''
    def __init__(self,screen_temp):
        self.x= random.choice(range(406))
        self.y=-74
        self.screen=screen_temp
        self.image=pygame.image.load("images/e"+str(random.choice(range(3)))+".png")
        self.bullet_list=[]# 敌机子弹列表

    def display(self):
        # 绘制子弹
        for b in self.bullet_list:
            b.display()
            if b.move():  # 子弹移出屏幕后，销毁子弹
                self.bullet_list.remove(b)
        self.screen.blit(self.image,(self.x,self.y))

    def fire(self):
        self.bullet_list.append(Bullet_Enemy(self.screen,self.x,self.y))
        # print(len(self.bullet_list))

    def move(self,hero):
        self.y+=4
        # 敌机随机发射子弹
        if random.choice(range(50))==10:
            self.fire()
        # 敌机移出屏幕
        if self.y>516:
            return True
        # 与玩家子弹碰撞检测
        for blt in hero.bullet_list:
            if blt.x>self.x+12 and blt.x<self.x+92 and blt.y>self.y+20 and blt.y<self.y+70:
                hero.bullet_list.remove(blt)
                return True

class Bump:
    '''爆炸场景'''
    def __init__(self,screen_temp,x,y):
        self.x=x
        self.y=y
        self.screen=screen_temp
        for i in range(1,4):
            print("爆炸",i)
            self.image=pygame.image.load("images/bomb_0"+str(i)+".png")
            self.screen.blit(self.image, (self.x, self.y))

def key_control(hero_temp):
    '''键盘控制函数'''
    # 执行退出操作
    for event in pygame.event.get():
        if event.type==QUIT:
            exit()
    #获取按键信息
    pressed_keys=pygame.key.get_pressed()
    # 根据按键，判断执行操作
    if pressed_keys[K_LEFT] or pressed_keys[K_a]:
        print("left...")
        hero_temp.move_left()

    elif pressed_keys[K_RIGHT] or pressed_keys[K_d]:
        print("right...")
        hero_temp.move_right()

    if pressed_keys[K_SPACE]:
        print("space")
        hero_temp.fire()

def gameOver(screen_temp):
    overfont=pygame.font.SysFont('arial',26)
    text=overfont.render("Esc Quit Game or Enter Again!",True,(53,17,233))
    textRect=text.get_rect()
    textRect.center=(200,400)
    background = pygame.image.load("images/over.jpg")
    while True:
        pygame.transform.scale(background, (200, 200))
        screen_temp.blit(background,(6,100))
        screen_temp.blit(text, textRect)
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
        pressed_keys = pygame.key.get_pressed()
        # 根据按键，判断执行操作
        if pressed_keys[K_ESCAPE]:
            return True
        elif pressed_keys[K_RETURN]:
            return False
        # 更新显示
        pygame.display.update()

def main():
    '''主程序函数'''
    pygame.init()
    # 创建游戏窗体
    screen=pygame.display.set_mode((512,568),0,0)
    # 创建游戏背景
    background=pygame.image.load("images/bg2.png")

    # 创建玩家飞机
    heroPlane=HeroPlane(screen)
    pos=-968 #背景位置
    enemyList=[]#敌机列表
    while True:
        # 绘制画面
        screen.blit(background,(0,pos))
        pos+=2
        if pos>=-200:
            pos=-968
        # 绘制玩家飞机
        heroPlane.display()
        # 执行键盘控制
        key_control(heroPlane)
        # 绘制敌机
        if random.choice(range(50))==10:
            enemyList.append(EnemyPlane(screen))
        for em in enemyList:
            em.display()
            if em.move(heroPlane):# 敌机移出屏幕 或 与子弹碰撞
                Bump(em.screen,em.x+40, em.y+20) # 敌机爆炸
                enemyList.remove(em) # 敌机销毁

        # 敌机子弹与玩家检测碰撞
        isHeroBump=heroPlane.checkBump(enemyList)
        if isHeroBump:# 玩家战败
            Bump(heroPlane.screen,heroPlane.x+40,heroPlane.y+20)
            del heroPlane
            break

        # 更新显示
        pygame.display.update()
        # 定时显示画面
        time.sleep(0.05)
    isover=gameOver(screen)
    if isover:
        quit()
    else:
        main()

if __name__=="__main__":
    main()